<template>
  <div>
    <Form
      :schema="schema"
      :model="intoOut"
      @register="formRegister"
      :isCol="false"
      label-width="240px"
    />
    <div v-if="readonly == false" style="text-align: center; margin-top: 10px; width: 100%">
      <el-space>
        <BaseButton type="primary" @click="saveInto">保存</BaseButton>
        <BaseButton @click="initIntoOut">重置</BaseButton>
      </el-space>
    </div>
  </div>
</template>

<script>
import { GetFormId } from '@/flow/api/flow'
import { EnumDic, FlowNodeType } from '@/flow/config/flowConfig'
import * as flowNodeApi from '@/flow/api/flowNode'
import nodeFormulaEdit from './nodeFormulaEdit.vue'
import { useForm } from '@/hooks/web/useForm'
import { Form } from '@/components/Form'
const { formRegister, formMethods } = useForm()
import { h } from 'vue'
export default {
  components: {
    Form
  },
  props: {
    nodeId: {
      type: String,
      default: null
    },
    defFormId: {
      type: String,
      default: null
    },
    flowId: {
      type: String,
      default: null
    },
    source: {
      type: Object,
      default: null
    },
    isload: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EnumDic,
      intoOut: {},
      formId: [],
      formulaList: [],
      schema: [],
      rules: {
        IntoMinPassNum: [
          {
            required: true,
            message: '进入的最小通过数不能为空！',
            trigger: 'blur'
          }
        ],
        OutMinPassNum: [
          {
            required: true,
            message: '子流程的最小通过数不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isload: {
      handler(val) {
        if (val) {
          this.initIntoOut()
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    formRegister,
    initScheme() {
      const list = [
        {
          field: 'start',
          label: '节点启动策略',
          component: 'Divider'
        },
        {
          field: 'StartMode',
          label: '节点启动方式',
          component: 'EnumItem',
          componentProps: {
            readonly: this.readonly,
            dicKey: EnumDic.FlowNodeStartMode,
            sysHead: 'flow',
            placeholder: '节点启动方式',
            on: {
              change: this.startChange
            }
          }
        }
      ]
      if (this.intoOut.StartMode === 2) {
        list.push({
          field: 'DelayTime',
          label: '延迟时间',
          component: 'InputNumber',
          componentProps: {
            style: {
              width: '300px'
            },
            controlsPositio: 'right',
            placeholder: '延迟时间(分钟)'
          }
        })
      } else if (this.intoOut.StartMode === 1) {
        list.push({
          field: 'StartColId',
          label: '读取启动时间',
          component: 'FormColChiose',
          componentProps: {
            controlsPositio: 'right',
            formId: this.formId,
            clearable: true,
            filterDataType: [7, 8],
            placeholder: '读取启动时间的表单字段',
            on: {
              change: (val, form) => {
                this.intoOut.StartFormId = form ? form.FormId : null
              }
            }
          }
        })
      }
      list.push({
        field: 'out',
        label: '出方向策略',
        component: 'Divider'
      })
      list.push({
        field: 'OutPolicy',
        label: '存在多个可入节点时',
        component: 'EnumItem',
        componentProps: {
          readonly: this.readonly,
          dicKey: EnumDic.FlowNodeOutPolicy,
          sysHead: 'flow',
          placeholder: '存在多个可入节点时处理方式'
        }
      })
      list.push({
        field: 'OutCondition',
        label: '节点出条件',
        component: 'EnumItem',
        componentProps: {
          readonly: this.readonly,
          dicKey: EnumDic.FlowOutCondition,
          sysHead: 'flow',
          placeholder: '节点出条件',
          on: {
            change: (val) => {
              this.intoOut.OutCondition = val
              this.initScheme()
            }
          }
        }
      })
      list.push({
        field: 'OutNoPassOpt',
        label: '未通过时的处理方式',
        component: 'EnumItem',
        componentProps: {
          readonly: this.readonly,
          dicKey: EnumDic.AuditNoPassOpt,
          sysHead: 'flow',
          placeholder: '审批未通过时的处理方式'
        }
      })
      if (this.intoOut.OutCondition !== 0) {
        list.push({
          field: 'OutIsWaitPass',
          label: '是否等待子流程通过',
          component: 'Switch',
          componentProps: {
            disabled: this.readonly,
            activeText: '是',
            inactiveText: '否'
          }
        })
      }
      if (this.intoOut.OutCondition === 3 || this.intoOut.OutCondition === 2) {
        list.push({
          field: 'OutMinPassNum',
          label: '子流程最小通过数',
          component: 'InputNumber',
          componentProps: {
            disabled: this.readonly,
            placeholder: '子流程最小通过数'
          }
        })
      }
      if (this.intoOut.OutCondition === 2) {
        list.push({
          field: 'OutWaitNodeId',
          label: '选择需完成的子流程',
          component: 'Select',
          componentProps: {
            clearable: true,
            disabled: this.readonly,
            options: this.filterSubNode(),
            props: {
              label: 'NodeName',
              value: 'Id'
            },
            placeholder: '选择需完成的子流程'
          }
        })
      }
      list.push({
        field: 'out',
        label: '入方向策略',
        component: 'Divider'
      })
      list.push({
        field: 'IntoCondition',
        label: '节点进入条件',
        component: 'EnumItem',
        componentProps: {
          readonly: this.readonly,
          dicKey: EnumDic.FlowNodeIntoCondition,
          sysHead: 'flow',
          placeholder: '节点进入条件',
          on: {
            change: (val) => {
              this.intoOut.IntoCondition = val
              this.initScheme()
            }
          }
        }
      })
      if (this.intoOut.IntoCondition === 3 || this.intoOut.IntoCondition === 2) {
        list.push({
          field: 'IntoMinPassNum',
          label: '进入的最小通过数',
          component: 'InputNumber',
          componentProps: {
            disabled: this.readonly,
            placeholder: '进入的最小通过数'
          }
        })
      }
      if (this.intoOut.IntoCondition === 3) {
        list.push({
          field: 'IntoWaitNodeId',
          label: '选择需完成的上级流程',
          component: 'Select',
          componentProps: {
            clearable: true,
            disabled: this.readonly,
            options: this.source.FromNode,
            props: {
              label: 'NodeName',
              value: 'Id'
            },
            placeholder: '选择需完成的子流程'
          }
        })
      }
      this.formulaList.forEach((c) => {
        list.push({
          label: c.NodeName + '节点条件公式',
          formItemProps: {
            slots: {
              default: () => {
                return h(nodeFormulaEdit, {
                  nodeName: c.NodeName,
                  modelValue: c.formula,
                  readonly: this.readonly,
                  nodeId: c.NodeId,
                  defFormId: this.defFormId,
                  'onUpdate:modelValue': (tv) => {
                    c.formula = tv
                  }
                })
              }
            }
          }
        })
      })
      this.schema = list
    },
    async startChange(mode) {
      this.intoOut.StartMode = mode
      if (this.intoOut.StartMode === 1 && this.formId.length === 0) {
        this.formId = await GetFormId(this.flowId)
      }
      this.initScheme()
    },
    filterSubNode() {
      if (this.source.ToNode == null || this.source.ToNode.length === 0) {
        return []
      }
      return this.source.ToNode.filter((c) => c.NodeType === FlowNodeType.subFlow.value)
    },
    initIntoOut() {
      this.intoOut = {
        StartMode: this.source.StartMode,
        StartFormId: this.source.StartFormId,
        StartColId: this.source.StartColId,
        DelayTime: this.source.DelayTime,
        IntoCondition: this.source.IntoCondition,
        IntoMinPassNum: this.source.IntoMinPassNum,
        OutIsWaitPass: this.source.OutIsWaitPass,
        IntoWaitNodeId: this.source.IntoWaitNodeId,
        OutPolicy: this.source.OutPolicy,
        OutCondition: this.source.OutCondition,
        OutMinPassNum: this.source.OutMinPassNum,
        OutNoPassOpt: this.source.OutNoPassOpt,
        OutWaitNodeId: this.source.OutWaitNodeId
      }
      if (this.source.FromNode != null) {
        this.formulaList = this.source.FromNode.map((c) => {
          const data = {
            NodeId: c.Id,
            NodeName: c.NodeName,
            FormId: c.FormId,
            formula: null
          }
          if (this.source.IntoFormula != null) {
            data.formula = this.source.IntoFormula.find((a) => a.NodeId === c.Id)
          }
          return data
        })
      } else {
        this.formulaList = []
      }
      formMethods.setValues(this.intoOut)
      this.initScheme()
    },
    async saveInto() {
      const data = await formMethods.validate()
      if (data == null) {
        return
      }
      this.saveIntoOut(Object.assign({}, data))
    },
    async saveIntoOut(data) {
      if (this.formulaList != null && this.formulaList.length > 0) {
        const list = this.formulaList.filter((a) => a.formula != null && a.formula.Formula != null)
        data.IntoFormula = list.map((a) => a.formula)
      }
      await flowNodeApi.SetIntoOut(this.nodeId, data)
      this.$message({
        message: '更新成功!',
        type: 'success'
      })
      this.$emit('change', data)
    }
  }
}
</script>
